package com.rocbrid.order.service.impl;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.rocbrid.order.bean.Order;
import com.rocbrid.order.feign.ProductFeignClient;
import com.rocbrid.order.service.OrderService;
import com.rocbrid.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;  //导入<artifactId>spring-cloud-starter-loadbalancer</artifactId>
    @Autowired
    ProductFeignClient productFeignClient; //使用feignClient来远程调用

    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallback")
    @Override
    public Order createOrder(Long userId, Long productId) {
     //   Product product = getProductFromRemoteWithLoadBalancerAnnotation(productId);
        Product product = productFeignClient.getProduct(productId); //使用Feign远程调用
        Order order = new Order();
        order.setId(1L);
        //  商品总净额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("ZHANGSAN");
        order.setAddress("西安");
        //  远程查询商品列表
        order.setProductList(Arrays.asList(product));
        return order;
    }

    //@SentinelResource的兜底异常
    public Order createOrderFallback(Long userId, Long productId, BlockException e) {
        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("未知商品");
        order.setAddress(e.getMessage());

    return order;
    }


    //远程调用进阶二:带有负载均衡功能的远程调用 @LoadBalanced 标注后restTemplate自带负载均衡功能
    private Product getProductFromRemoteWithLoadBalancerAnnotation(Long productId) {
        String url = "http://service-product/product/4" + productId;
        log.info("远程请求路径{}", url);
        //给远程发送请求service-product被动态替换
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    //远程调用进阶一:带有负载均衡功能的远程调用
    private Product getProductFromRemoteWithLoadBalancer(Long productId) {
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        //http://localhost:9003/product/4
        String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/product/" + productId;
        log.info("远程请求路径{}", url);
        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    //最基础的远程调用
    private Product getProductFromRemote(Long productId) {
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        //http://localhost:9003/product/4
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求路径{}", url);
        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}

